package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.bean.UserReuseBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.ReuseUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import com.koron.util.ValidationKey;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 用户复用增加
 * @author 甄玉晴
 *
 */
public class ReuseUserAdd implements ServerInterface {

	@Override
	@ValidationKey(clazz = UserReuseBean.class,method = "insert")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<Map> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", Map.class);
		try {
			ReuseUserMapper mapper = factory.getMapper(ReuseUserMapper.class);
			UserReuseBean bean = JsonUtils.objectToPojo(req.getData(), UserReuseBean.class);
			// 获取复用信息
			int ingReuse = mapper.userReuseIng(bean.getUserNo());
			if(ingReuse>0) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "该用户编号正在复用中，不能再次复用", void.class);
			}
			bean.setCreateInfo(userInfo);
			bean.setBillNo(Tools.getBillNo(factory, "USER_REUSE"));
			bean.setBillDate(Tools.getCurrentDate());
			//查询用户水表是否已拆表，如果拆表了，则需要复表
			String meterStatus=mapper.userMeterStatus(bean.getUserNo());
			if(!"2".equals(meterStatus)) {
				bean.setReuseMeterFlag(1);
			}
			//记录复用信息
			mapper.userReuseAdd(bean);
			//设置用户变更事项。只显示变更用户状态
			List<UserChangeItemBean> item = new ArrayList<UserChangeItemBean>();
			UserChangeItemBean ubean = new UserChangeItemBean();
			ubean.setReceiptId(bean.getId());
			ubean.setReceiptType("USER_REUSE");
			ubean.setUserNo(bean.getUserNo());
			ubean.setBillNo(bean.getBillNo());
			ubean.setCreateInfo(userInfo);
			ubean.setChangeItem("用户状态");
			ubean.setOldItemValue("2");
			ubean.setNewItemValue("1");
			item.add(ubean);
			ChangeUserMapper cmapper = factory.getMapper(ChangeUserMapper.class);
			cmapper.userChangeItemAdd(item, 2);
			//返回ID
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", bean.getId());
			msg.setData(map);
			BaseReceiptAccessoryMapper accessoryMapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
			if(!StringUtils.isBlank(bean.getTempId()))		//修改附件ID
				accessoryMapper.updateAccessoryReceiptId(bean.getId(), bean.getTempId());
			//输出执行结果
			return msg;
		} catch (Exception e) {
			logger.error("复用信息增加失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "复用信息增加失败", void.class);
		}
	}

}
